package com.yj.generator.core;

import com.yj.generator.entity.Comment;
import com.yj.generator.entity.GenerateOut;
import com.yj.generator.entity.Table;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;


/**
 * @author DengLin
 * @title 文件生成
 * @date 2024/07/05
 */

public class GenerateFileProxy {
    private static final Logger log = LogManager.getLogger(GenerateFileProxy.class);

    private static final Map<String,GenerateFile> GENERATE_FILE_MAP = new HashMap<>();

    static {
        GENERATE_FILE_MAP.put("Bean",new BeanGenerateFileImpl());
    }

    public void generate(GenerateOut generateOut,Table table){
        String author = generateOut.getAuthor();
        Comment comment = new Comment();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        comment.setAuthor(author);
        comment.setDate(dateFormat.format(new Date()));

        String outPackage = generateOut.getOutPackage();
        Map<String, Object> model = new HashMap<>();
        model.put("remark",comment);
        model.put("packageName",outPackage);
        model.put("table",table);

        for (Map.Entry<String, GenerateFile> entry : GENERATE_FILE_MAP.entrySet()) {
            log.info("开始生成:{}",table.getBeanName()+entry.getKey());
            GenerateFile generateFile = entry.getValue();
            generateFile.generate(generateOut,model);
        }

    }

}
